package com.btex.app.pager;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.btex.app.Config;
import com.btex.app.LoginActivity;
import com.btex.app.R;
import com.btex.app.adapter.MyFragmentAdapter;
import com.btex.app.entity.MyInfoListInfo;
import com.btex.app.tools.SharedPreferencesUtil;
import com.btex.app.treaty.ContractAssetsFragment;
import com.lidroid.xutils.util.PreferencesCookieStore;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by wcx on 16/3/16.
 */
public class FragmentTrade extends BaseFragment {
    private static final String TAG = "FragmentTrade";

    Unbinder unbinder;
    @BindView(R.id.fragment_trade_viewpager)
    ViewPager mFragmentTradeViewpager;
    @BindView(R.id.treaty_tv_bibi)
    TextView mTreatyTvBibi;
    @BindView(R.id.treaty_tv_heyu)
    TextView mTreatyTvHeyu;


    private PreferencesCookieStore cookieStore;

    private List<MyInfoListInfo.DataBean.CoinsBean> mCoins;
    private boolean mIsHide;
    private SharedPreferencesUtil mSharedPreferences;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_list_property, null);
        cxt = getActivity();
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mSharedPreferences = SharedPreferencesUtil.getSharedPreferences(getActivity());
        mIsHide = mSharedPreferences.getBoolean("isHide", false);
        boolean isLogin = mSharedPreferences.getBoolean(Config.KEY_IS_LOGIN, false);
        mTreatyTvBibi.setSelected(true);
        initViews();

        initOnclik();

    }

    private void initViews() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new ContractAssetsFragment());
//        fragments.add(new CoinsFragment());
        ArrayList<String> strings = new ArrayList<>();
        strings.add("");
//        strings.add("已成交");
//        strings.add("已平仓位");

        mFragmentTradeViewpager.setAdapter(new MyFragmentAdapter(getActivity().getSupportFragmentManager(), fragments, strings));
        mFragmentTradeViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


            }

            @Override
            public void onPageSelected(int position) {

                if (position == 0) {
                    setStat();
                    mTreatyTvBibi.setSelected(true);
                } else {
                    setStat();
                    mTreatyTvHeyu.setSelected(true);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initOnclik() {
        mTreatyTvBibi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setStat();
                mTreatyTvBibi.setSelected(true);

                mFragmentTradeViewpager.setCurrentItem(0, true);
            }
        });

        mTreatyTvHeyu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setStat();
                mTreatyTvHeyu.setSelected(true);
                mFragmentTradeViewpager.setCurrentItem(1, true);
            }
        });

    }


    public void setStat() {
        mTreatyTvBibi.setSelected(false);
        mTreatyTvHeyu.setSelected(false);

    }


    @Override
    public void setData() {


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (mSharedPreferences != null) {
                boolean aBoolean = mSharedPreferences.getBoolean(Config.KEY_IS_LOGIN, false);
                if (!aBoolean) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            }

        }

    }
}
